/*
 * @lc app=leetcode.cn id=131 lang=java
 *
 * [131] 分割回文串
 */

// @lc code=start
import java.util.*;
class Solution {

    public List<List<String>> partition(String s) {
        if(s == null || s.length() == 0) {
            return res;
        }

        backtracking(s, 0, new StringBuilder());

        return res;
    }

    List<List<String>> res = new ArrayList<>();
    List<String> path = new LinkedList<>();
    private void backtracking(String s, int startIndex, StringBuilder sb) {
        if(startIndex == s.length()) {
            res.add(new ArrayList<>(path));
            return;
        }

        for(int i = startIndex; i < s.length(); i++) {
            sb.append(s.charAt(i));
            if(isPalindrome(sb)) {
                path.add(sb.toString());
                backtracking(s, i+1, new StringBuilder());
                path.remove(path.size() - 1);

            }
        }
    }

    private boolean isPalindrome(StringBuilder sb) {
        for(int i = 0; i < sb.length() / 2; i++) {
            if(sb.charAt(i) != sb.charAt(sb.length()-1-i)) {
                return false;
            }
        }
        return true;
    }

}
// @lc code=end

